<?php


namespace Message\Sensor;

use BatchConsumer;
use DebugConsumer;
use SensorsAnalytics;

class Sensor
{

    /**
     * @var SensorsAnalytics
     */
    private static $sa = null;

    private static function init()
    {
        $debug = env("sensor_debug", false);
        $url = self::getSensorLocation();
        $SA_DEBUG_WRITE_DATA = True;
        $SA_REQUEST_TIMEOUT = 1000;
        if ($debug) {
            $sensorConsumer = new DebugConsumer($url, $SA_DEBUG_WRITE_DATA, $SA_REQUEST_TIMEOUT);
        } else {
            $sensorConsumer = new BatchConsumer($url, $SA_DEBUG_WRITE_DATA, $SA_REQUEST_TIMEOUT);
        }
        self::$sa = new SensorsAnalytics($sensorConsumer);
    }

    public static function destruct()
    {
        if (self::$sa) {
            self::$sa->close();
        }
    }

    private static function getSensorLocation()
    {
        $sensor_url = env("sensor_url", '');
        return $sensor_url;
    }

    public static function track($distinct_id, $is_login_id, $event_name, $properties = array())
    {
        if (self::$sa === null) {
            self::init();
        }
        return self::$sa->track($distinct_id, $is_login_id, $event_name, $properties);
    }

    public static function profile_set($distinct_id, $is_login_id, $profiles = array())
    {
        if (self::$sa === null) {
            self::init();
        }
        return self::$sa->profile_set($distinct_id, $is_login_id, $profiles);
    }

    public static function track_signup($distinct_id, $original_id, $properties = array())
    {
        if (self::$sa === null) {
            self::init();
        }
        return self::$sa->track_signup($distinct_id, $original_id, $properties);
    }

}